local RxString = {}

-- 不是普通字符的处理(中文)

-- 入口
function RxString:isUtf8(str)
  if not str then return false end
  str = str or ''
  local strcode = utf8.codepoint(str)
  if strcode > 127 then
    return true
  end
  return false
end

function RxString:find(s1, s2)
  s1 = s1 or ''
  s2 = s2 or ''
  return string.find(s1, s2)
end

function RxString:sub(str, startChar, numChars)
  str = str or ''

  if numChars < startChar then return '' end

  startChar = startChar or 1
  numChars = numChars or 1
  if not str and str == '' then return -1 end

  local isUtf8 = RxString:isUtf8(str)
  if isUtf8 then
    RxLog('RxString:sub to utf8')
    return RxString:utf8Sub(str, startChar, numChars)
  end

  RxLog('RxString:sub -- to - string')
  return string.sub(str, startChar, numChars)
end

function RxString:len(str)
  if not str and str == '' then return 0 end
  if type(str) ~= 'string' then return 0 end
  return getStrNum(str)
end

function RxString:utf8Sub(str, startChar, numChars)
  local startIndex = 1
  local charStr = ''
  for index, code in utf8.codes(str) do
    if startIndex >= startChar and startIndex <= numChars then
      local char = utf8.char(code)
      charStr = charStr..tostring(char)
    end
    if startIndex > numChars then
      break
    end
    startIndex = startIndex + 1
  end
  return charStr
end

function RxString:utf8Find(s1,s2)
  local s1Indexs,s1Values,s1Len = RxString:utf8LoopKeyValue(s1)
  local s2Indexs,s2Values,s1Len = RxString:utf8LoopKeyValue(s2)

  -- 未完成
end

function RxString:utf8LoopKeyValue(str)
  local len = 0

  local listIndex = {}
  local listValue = {}
  for index, code in utf8.codes(str) do
    len = len + 1
    listIndex[len] = index
    listValue[len] = code
  end
  return listIndex,listValue,len
end

return RxString